package com.qishuihe.handler;

import com.qishuihe.common.utils.threads.*;
import com.qishuihe.running.runningrespository.tablecarddata.entity.TableCardDataEntity;
import com.qishuihe.running.runningrespository.tablecarddata.impl.TableCardDataImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;


public class TableCardHdler {

   private MegerDataCallable megerDataCallable;
   private MegerDataRunnable megerDataRunnable;
   private MegerDataThread megerDataThread;
    private MegerDataVO<List<Map<String,String>>> MegerDataVO;

    void queryTableCardData(){

       //假设现在有100W条数据在ls里面
       List<TableCardDataEntity> ls = initTableCardDataEntitys(17);
       //获取线程池工具类
       ThreadPoolTools threadPoolTools  = ThreadPoolTools.getThreadPoolTools();
       Map<Integer,List<TableCardDataEntity>> map  =  new HashMap<Integer,List<TableCardDataEntity>>();
       int count = ls.size()/threadPoolTools.getCorePoolSize();
       for(int i=0;i<threadPoolTools.getCorePoolSize(); i++){
           if(i==0){
               map.put(i,ls.subList(0,(i+1)*count));
           }else{
               map.put(i,ls.subList((i)*count,(i+1)*count));
           }
       }
       int mod = ls.size()%threadPoolTools.getCorePoolSize();
       if(mod!=0)
       {
           map.put(threadPoolTools.getCorePoolSize(),ls.subList(threadPoolTools.getCorePoolSize()*count,ls.size()));
       }

       //创建线程实例
//        creatMegerDataCallables(map,threadPoolTools);
          createMegerDataRunnables(map ,threadPoolTools);
    }
    void createMegerDataRunnables(Map<Integer,List<TableCardDataEntity>> map ,ThreadPoolTools threadPoolTools ){

       List<Future<MegerDataVO<List<Map<String,String>>>>> ls = new ArrayList<>();
       for(Integer key:map.keySet())
       {
           MegerDataVO<List<TableCardDataEntity>> megerDataVO = new  MegerDataVO<List<TableCardDataEntity>>();
           megerDataVO.setObj(map.get(key));
           megerDataVO.setOrder(key);
           System.out.println("MegerDataRunnable input："+megerDataVO.getOrder());
           MegerDataRunnable megerDataRunnable = new MegerDataRunnable(megerDataVO);
           ThreadPoolExecutor threadPoolExecutor = threadPoolTools.getThreadPoolExecutor();
           threadPoolExecutor.submit(megerDataRunnable);
//           MegerDataVO<List<Map<String,String>>> megerDataVO_ = megerDataRunnable.getMegerDataVO();
//           ls.add(futureTask);
       }
       for(Future<MegerDataVO<List<Map<String,String>>>> megerDataVO:ls){
           System.out.println("size:"+ls.size());
       }
    }
    List<MegerDataVO<List<Map<String,String>>>> creatMegerDataCallables(Map<Integer,List<TableCardDataEntity>> map ,ThreadPoolTools threadPoolTools ){
        List<Future<MegerDataVO<List<Map<String,String>>>>> lsFuture = new ArrayList<Future<MegerDataVO<List<Map<String,String>>>>>();

        List<MegerDataVO<List<Map<String,String>>>> tableCardDataEntitys = new ArrayList<MegerDataVO<List<Map<String,String>>>>();
        for(Integer key:map.keySet())
        {
            MegerDataVO<List<TableCardDataEntity>> megerDataVO = new  MegerDataVO<List<TableCardDataEntity>>();
            megerDataVO.setObj(map.get(key));
            megerDataVO.setOrder(key);
            System.out.println("input:"+key);
            Future<MegerDataVO<List<Map<String,String>>>> futureTask = threadPoolTools.getThreadPoolExecutor().submit(new MegerDataCallable(new TableCardDataImpl(),megerDataVO));
            lsFuture.add(futureTask);
        }
        for(Future<MegerDataVO<List<Map<String,String>>>> futureTask:lsFuture) {
            try {
                tableCardDataEntitys.add(futureTask.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        tableCardDataEntitys.sort((x,y)->Integer.compare(x.getOrder(),y.getOrder()));
        return tableCardDataEntitys;
    }
    List<TableCardDataEntity> initTableCardDataEntitys(int size){

        Map<Integer,String> codes = new HashMap<Integer, String>();
        codes.put(0,"name");
        codes.put(1,"sex");
        codes.put(2,"sco");
        codes.put(3,"level");
        codes.put(4,"dept");
        codes.put(5,"type");
        codes.put(6,"offering");
        codes.put(7,"skll");
        codes.put(8,"age");
        codes.put(9,"salary");
        List<TableCardDataEntity> ls = new ArrayList<TableCardDataEntity>();
       for(int i=0;i<size;i++)
       {
           TableCardDataEntity tableCardDataEntity = new TableCardDataEntity();
           tableCardDataEntity.setUuid("uuid"+i);
           tableCardDataEntity.setCode("code"+"uuid");
           tableCardDataEntity.setValue("uuid"+i);
           ls.add(tableCardDataEntity);
           for(int count=0;count<10;count++){
               tableCardDataEntity = new TableCardDataEntity();
               tableCardDataEntity.setUuid("uuid"+i);
               tableCardDataEntity.setCode("code"+codes.get(count));
               tableCardDataEntity.setValue("value"+i+count);
               ls.add(tableCardDataEntity);
           }

       }
       return ls;
    }


    public static void main(String[] args) {
        TableCardHdler tableCardHdler = new TableCardHdler();
        tableCardHdler.queryTableCardData();
    }
}
